﻿// **************************************************************************
// **************************************************************************
// ** Created by Igor Kozlov                                               **
// **************************************************************************
// **************************************************************************
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MagicControls
{
    public delegate void SizeChangedEventHandler(object o, SizeChangedEventArg eventArg);

    public class CanvasBase : Canvas, IMagicControl
    {
        private readonly RectangleGeometry canvasBoundary = new RectangleGeometry();

        #region IMagicControl Members

        public new virtual double Height
        {
            get { return base.Height; }
            set
            {
                base.Height = value;
                canvasBoundary.Rect = new Rect(0, 0, Width, Height);
                if (SizeChanged != null)
                    SizeChanged(this, new SizeChangedEventArg(base.Width, base.Height));
            }
        }

        public new virtual double Width
        {
            get { return base.Width; }
            set
            {
                base.Width = value;
                canvasBoundary.Rect = new Rect(0, 0, Width, Height);
                if (SizeChanged != null)
                    SizeChanged(this, new SizeChangedEventArg(base.Width, base.Height));
            }
        }

        #endregion

        public CanvasBase()
        {
            Loaded += CanvasBase_Loaded;
        }

        void CanvasBase_Loaded(object sender, System.EventArgs e)
        {
            canvasBoundary.Rect = new Rect(0, 0, Width, Height);
            Clip = canvasBoundary;
        }

        public event SizeChangedEventHandler SizeChanged;
    }
}